package com.lcf.user.utils;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ZkUtil {
    Logger logger = LoggerFactory.getLogger(ZkUtil.class);

    public static void setZknode(CuratorFramework client, String path, String data) {
        Stat stat = null;
        try {
            stat = client.checkExists().forPath(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (null != stat) {
                client.setData().forPath(path, data.getBytes());
            } else {
                client.create().creatingParentsIfNeeded().forPath(path, data.getBytes());

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    // 初始化curator client
    public CuratorFramework initCuratorClient(String connectString, String namespace) {
        // namespace是指根节点，重试策略：重试3次每次间隔10s
        //  创建的节点路径：/namespace/given-path
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder().connectString(connectString)
                .namespace(namespace)
                .sessionTimeoutMs(6000)
                .connectionTimeoutMs(6000)
                .retryPolicy(new RetryNTimes(3, 10))
                .build();
        curatorFramework.start();
        return curatorFramework;
    }


    // 创建节点
    public void createNode(CuratorFramework curatorClient, String path, String jsonStr) throws Exception {
        // 必须先保证节点不存在
        if (curatorClient.checkExists().forPath(path) == null) {
            // 递归创建节点
            curatorClient.create().creatingParentsIfNeeded().forPath(path, jsonStr.getBytes());
        } else {
            logger.warn(String.format("node already existed:[%s]", path));
        }
    }


    // 读取节点数据
    public byte[] getNodeData(CuratorFramework curatorClient, String path) throws Exception {
        byte[] data = null;
        if (curatorClient.checkExists().forPath(path) != null) {
            data = curatorClient.getData().forPath(path);
        } else {
            logger.warn(String.format("node does not exists:[%s]", path));
        }
        return data;
    }


    //  修改节点数据
    public void setNodeData(CuratorFramework curatorClient, String path, String jsonStr) throws Exception {
        // 必须先保证节点存在
        if (curatorClient.checkExists().forPath(path) != null) {
            curatorClient.setData().forPath(path, jsonStr.getBytes());
        } else {
            logger.warn(String.format("node does not exists:[%s]", path));
        }
    }


    // 删除节点
    public void deleteNode(CuratorFramework curatorClient, String path) throws Exception {
        // 必须先保证节点存在
        if (curatorClient.checkExists().forPath(path) != null) {
            // 递归删除节点
            curatorClient.delete().deletingChildrenIfNeeded().forPath(path);
        } else {
            logger.warn(String.format("node does not exists:[%s]", path));
        }
    }
}
